﻿using System;
using System.Text.RegularExpressions;
namespace BibtexParser.ParserModels
{
    public class StringRep
    {
        private readonly string _abbreviation;
        private readonly string _fullText;

        /**
		 * A representation of the Strings (that is to say the macros that have been made
		 * by the user), which later are to expand parts of the text (for example - if we 
		 * have the makro @STRING(CS="Computer Science"), then every instance of the word "CS"
		 * will be expanded into "Computer Science"
		 * @param abb 	The abbreviation that will later be expanded
		 * @param full	The thing the abbriviation will be expanded into
		 */
        public StringRep(String abb, String full)
        {
            _abbreviation = abb;

            _fullText = full;

            // replace = "(\\s+)" + abb + "(\\s*)";
        }

        /// <summary>
        /// Returns the abbreviation (not what it is supposed to be expanded into)
        /// </summary>
        /// <returns>The Abbreviation string</returns>
        public String getAbb()
        {
            return _abbreviation;
        }

        /// <summary>
        /// Returns the text the abbreviation is supposed to be expanded into
        /// </summary>
        /// <returns>Full text</returns>
        public String getFull()
        {
            return _fullText;
        }

        /// <summary>
        /// Replaces the abbreviations in the String with the fulltext
        /// </summary>
        /// <param name="str">String potentially containg an abbreviation</param>
        /// <returns>string with abbreviation replaced</returns>
        public String replacedString(String str)
        {
            var potMAtch = Regex.Match(str, _abbreviation);
            if (potMAtch.Success)
            {
                str = str.Replace(_abbreviation, "$1" + _fullText + "$2");
            }
            return str;
        }	
    }
}